<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>用户登录页面</title>
    <link rel="stylesheet" href="lib/bootstrap.min.css">
    <style>
        * {
            padding: 0;
            margin: 0;
        }
        
        html,
        body {
            height: 100%;
            width: 100%;
            overflow: hidden;
            background: url(img/bg2.jpg) no-repeat center;
            background-size: 100% 100%;
        }
        
        .wrap {
            width: 350px;
            height: 300px;
            margin: 100px auto;
            border-radius: 10px;
            background-color: rgba(255, 255, 255, .8);
        }
        
        .wrap h3 {
            margin: 0;
            height: 60px;
            line-height: 60px;
            font-family: "华文细黑";
            border-bottom: 1px solid #999999;
            text-indent: 16px;
        }
        
        .wrap form {
            padding: 12px;
            box-sizing: border-box;
        }
        
        .login {
            margin: 0 70px;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <h3>用户登录</h3>
        <form>
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" class="form-control" id="username" placeholder="请输入用户名">
            </div>
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" class="form-control" id="password" placeholder="请输入密码">
            </div>
            <div class="checkbox">
                <label>
                      <input type="checkbox" id="nologin">30天内免登录
                </label>
            </div>
            <button type="button" class="btn btn-primary login">登录</button>
            <button type="button" class="btn btn-danger register">注册</button>
        </form>
    </div>
    <script src="lib/jquery.min.js"></script>
    <script src="lib/common.js"></script>

    <script>
        $(".login").click(function() {
            if ($("#username").val() && $("#password").val()) {
                $.ajax({
                    url: `${address}/login`,
                    method: "POST",
                    data: {
                        username: $("#username").val(),
                        password: $("#password").val(),
                    },
                    success(data) {
                        if (data === "0") {
                            alert("用户登录失败！");
                        } else {
                            if (data === "1") {
                                alert("用户名或密码错误！");
                            } else {
                                if ($("#nologin")[0].checked === true) {
                                    //30天内免登录状态
                                    let date = new Date();
                                    date.setDate(date.getDate() + 30);
                                    document.cookie = `userInfo=${data};expires=${date}`;
                                    sessionStorage.setItem("userInfo", data);
                                }
                                //本地存储
                                sessionStorage.setItem("userInfo", data);
                                location.href = `${address}/student.html`;
                            }
                        }
                    }
                })
            } else {
                alert("请输入用户名或密码");
            }
        })
        $(".register").click(function() {
            location.href = `${address}/register.html`;
        })
    </script>
</body>

</html>